$(function() {
	var editor = CKEDITOR.replace('editor');

	editor.ui.addButton('uploadimg', {
		label: '上传图片',
		command: 'insertTimestamp',
		toolbar: 'insert,0'
	});
	editor.addCommand('insertTimestamp', {
		exec: function(editor) {
			var upload = document.createElement("input");
			upload.type = "file";
			upload.id = '_imgupload'
			upload.onchange = function() {
				var file = this.files[0];
				var data = new FormData();
				data.append("files", file);
				var picCheck = upPicCheck({file:file});
				if(picCheck.status != 1){
					_alert.error({
						title:'图片错误',
						msg:picCheck.msg
					});
					return;
				}
				// var reader = new FileReader();
				// reader.readAsDataURL(file); // 读出 base64
				// reader.onloadend = function () {       
				// 	var dataURL = reader.result;
				// 	editor.insertHtml( '<figure class="easyimage easyimage-full"><img alt="" src="'+dataURL+'" /><figcaption>图片标题</figcaption></figure>' );
				// };

		        $.ajax({
		            url: '/index/test/unifiedUpload/name/files',
		            type: 'POST',
		            cache: false,
		            data: data,
		            processData: false,
		            contentType: false,
		            dataType: "json",
		            beforeSend: function (res) {},
		            success: function (res) {
						if(res.error == 0){
							_alert.info('上传成功');
							editor.insertHtml( '<figure class="easyimage easyimage-full"><img alt="" src="https://dsj.gzshangning.com/'+res.url+'" /><figcaption>图片标题</figcaption></figure>' );
						}else{
							_alert.error({
								title:'上传失败',
								msg:'服务器异常文件上传失败,请刷新重试。'
							});
						}
		            }
		        });
			}
			upload.click();
			// editor.insertHtml( 	'The current date and time is: <em>' + now.toString() + '</em>' );

		}
	});


	$('.tagbtn .item').click(function() {

		var index = $(this).index('.tagbtn .item');
		if ($(this).hasClass('active')) {
			return
		}
		$('.tagbtn .item').removeClass('active');
		$(this).addClass('active')

	});
	$('.left .tagbox .item ul li').click(function(){
		editor.insertHtml($(this).html());
	});


	/*
	上传缩略图
	*/
	$('#uploadThumb').click(function(){
		uploadThumb(function(err,result){
			if(err){
				return;
			}
			console.log(result);
		});
	});


	/*
	上传文案
	*/
	$('#tijiao').click(function(){
		var data = {};
		$("#docupload").serializeArray().forEach(function(item){
			if(!data[item.name]){
				data[item.name] = item.value;
			}else{
				if(data[item.name] instanceof Array){
					data[item.name].push(item)
				}else{
					var arr = [data[item.name],item];
					data[item.name] = arr;
				}
			}
		});
		data.content = CKEDITOR.instances.editor.getData();
		console.log(data);
		$.ajax({
			url:"/index/user/save",
			type:"post",
			data:data,
			success:function(res){
				if(res.code == 0){
					_alert.info(res.msg,'1000');
				}else if(res.code == 1){
					_alert.success({title:'上传成功',msg:'文案上传成功，请耐心等待审核'})
				}
				console.log(res);
			}
		});
	});



	/**
	 * 
	 */
	setAddCont($('.docupload .left .tagbtn .item.active').attr('pid'))

	// setAddCont(5)
})